﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
namespace GUI.HangHoa
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion

		
        #region Khoi Tao
        private DateTime date;
        private Boolean GioiTinh = false;
        private string TenLoaiHang = string.Empty;
        private string MaLoaiHang = string.Empty;
        private string TenNhomHang = string.Empty;
        private string MaNhomHang = string.Empty;
        private string TenDonViTinh = string.Empty;
        private string MaDonViTinh = string.Empty;
        private string TenThue = string.Empty;
        private string MaThue = string.Empty;
        private Entities.GetCombobox[] phongban = null;
        private string MaHangHoa = string.Empty;
        private string links = string.Empty;
        private string linksRoot = string.Empty;
        private Image image;
        private byte[] bytes;

        private string hanhDong;
        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private Entities.HangHoa nhanvien;
        public Entities.HangHoa Nhanvien
        {
            get { return nhanvien; }
            set { nhanvien = value; }
        }
        public frmXuLy()
        {
            InitializeComponent();
        }
        public frmXuLy(string hanhDong, Entities.HangHoa nhanvien)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.nhanvien = nhanvien;
        }
        #endregion

        #region Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                LoaiHang();
                NhomHang();
                DonViTinh();
                Thue();
                date = Common.StaticVariable.InterfaceObject.SelectDate();
                switch (this.hanhDong)
                {
                    case "Them":
                        break;
                    case "Sua":
                        DoDuLieu();
                        break;
                    default:
                        this.Thoat();
                        break;
                }
                this.WindowState = FormWindowState.Normal;
                txtmahanghoa.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua Du Lieu
        private void DoDuLieu()
        {
            try
            {
                MaDonViTinh = nhanvien.MaDonViTinh;
                TenDonViTinh = nhanvien.TenDonViTinh;
                MaLoaiHang = nhanvien.MaLoai;
                TenLoaiHang = nhanvien.TenLoai;
                MaNhomHang = nhanvien.MaNhomHang;
                TenNhomHang = nhanvien.TenNhomHang;
                MaThue = nhanvien.MaThue;
                TenThue = nhanvien.GiaTriThue;
                HienThiLoaiHang(nhanvien.MaLoai);
                HienThiNhomHang(nhanvien.MaNhomHang);
                HienThiDonViTinh(nhanvien.MaDonViTinh);
                HienThiThue(nhanvien.MaThue);
                txtmahanghoa.Text = nhanvien.MaHangHoa;
                txttenhanghoa.Text = nhanvien.TenHangHoa;
                txtgianhap.Text = nhanvien.GiaNhap;
                txtgiabanbuon.Text = nhanvien.GiaBanBuon;
                txtgiabanle.Text = nhanvien.GiaBanLe;
                txtmucdathang.Text = nhanvien.MucDatHang;
                txttontoithieu.Text = nhanvien.MucTonToiThieu;
                txtLinkRoot.Text = nhanvien.LinkImageRoot;
                if (this.nhanvien.LinkImageRoot != string.Empty)
                {
                    bytes = Common.StaticVariable.InterfaceObject.GetImage(nhanvien.LinkImageRoot);
                    if (bytes != null)
                    {
                        image = Common.Utilities.GetImage(bytes);
                        picNhanVien.BackgroundImage = image;
                    }
                    else
                    {
                        bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                        image = Common.Utilities.GetImage(bytes);
                        picNhanVien.BackgroundImage = image;
                    }
                }
                else
                {
                    bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                    image = Common.Utilities.GetImage(bytes);
                    picNhanVien.BackgroundImage = image;
                }
                txtGhiChu.Text = nhanvien.GhiChu;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        CheckClose = true;
                        GetData(false);
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Loại hàng
        private void HienThiLoaiHang(string maloaihang)
        {
            try
            {
                for (int i = 0; i < phongban.Length; i++)
                {
                    if (phongban[i].TenCotKhoaChinh == maloaihang)
                    {
                        cbxloaihanghoa.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception)
            {
                cbxloaihanghoa.Items.Clear();
                MaLoaiHang = string.Empty;
                TenLoaiHang = string.Empty;
            }
        }
        private void LoaiHang()
        {
            try
            {
                cbxloaihanghoa.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "LoaiHang";
                pb.TenCotKhoaChinh = "MaLoaihang";
                pb.TenCotTenHienThi = "TenLoaiHang";
                phongban = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
                Common.Utilities.XuLyComboBox(cbxloaihanghoa, phongban, "TenCotTenHienThi", "TenCotKhoaChinh");
            }
            catch (Exception ex)
            {
                cbxloaihanghoa.Items.Clear();
                string s = ex.Message;
            }
        }
        #endregion

        #region Nhóm hàng
        private void HienThiNhomHang(string manhomhang)
        {
            try
            {
                for (int i = 0; i < phongban.Length; i++)
                {
                    if (phongban[i].TenCotKhoaChinh == manhomhang)
                    {
                        cbxnhomhanghoa.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception)
            {
                cbxnhomhanghoa.Items.Clear();
                MaNhomHang = string.Empty;
                TenNhomHang = string.Empty;
            }
        }
        private void NhomHang()
        {
            try
            {
                cbxnhomhanghoa.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "NhomHang";
                pb.TenCotKhoaChinh = "MaNhomHang";
                pb.TenCotTenHienThi = "TenNhomHang";
                phongban = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
                Common.Utilities.XuLyComboBox(cbxnhomhanghoa, phongban, "TenCotTenHienThi", "TenCotKhoaChinh");
            }
            catch (Exception ex)
            {
                cbxnhomhanghoa.Items.Clear();
                string s = ex.Message;
            }
        }
        #endregion

        #region Đơn vị tính
        private void HienThiDonViTinh(string madvt)
        {
            try
            {
                for (int i = 0; i < phongban.Length; i++)
                {
                    if (phongban[i].TenCotKhoaChinh == madvt)
                    {
                        cbxdonvitinh.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception)
            {
                cbxdonvitinh.Items.Clear();
                MaDonViTinh = string.Empty;
                TenDonViTinh = string.Empty;
            }
        }
        private void DonViTinh()
        {
            try
            {
                cbxdonvitinh.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "DonViTinh";
                pb.TenCotKhoaChinh = "MaDonViTinh";
                pb.TenCotTenHienThi = "TenDonViTinh";
                phongban = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
                Common.Utilities.XuLyComboBox(cbxdonvitinh, phongban, "TenCotTenHienThi", "TenCotKhoaChinh");
            }
            catch (Exception ex)
            {
                cbxdonvitinh.Items.Clear();
                string s = ex.Message;
            }
        }
        #endregion

        #region thuế
        private void HienThiThue(string mathue)
        {
            try
            {
                for (int i = 0; i < phongban.Length; i++)
                {
                    if (phongban[i].TenCotKhoaChinh == mathue)
                    {
                        cbxthue.SelectedIndex = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception)
            {
                cbxthue.Items.Clear();
                MaThue = string.Empty;
                TenThue = string.Empty;
            }
        }
        private void Thue()
        {
            try
            {
                cbxthue.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "Thue";
                pb.TenCotKhoaChinh = "MaThue";
                pb.TenCotTenHienThi = "GiaTriThue";
                phongban = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
                Common.Utilities.XuLyComboBox(cbxthue, phongban, "TenCotTenHienThi", "TenCotKhoaChinh");
            }
            catch (Exception ex)
            {
                cbxthue.Items.Clear();
                string s = ex.Message;
            }
        }
        #endregion

        #region Quyen
        private Boolean Quyen()
        {
            return true;
        }
        #endregion

        #region Gia Tri Them
        private Entities.HangHoa LayBanGhi(string hanhdong)
        {
            Entities.HangHoa nv = new Entities.HangHoa();
            try
            {
                nv.HanhDong = hanhdong;
                nv.MaHangHoa = txtmahanghoa.Text;
                nv.TenHangHoa = txttenhanghoa.Text.Trim();
                nv.TenLoai = this.TenLoaiHang;
                nv.MaLoai = this.MaLoaiHang;
                nv.TenNhomHang = this.TenNhomHang;
                nv.MaNhomHang = this.MaNhomHang;
                nv.TenDonViTinh = this.TenDonViTinh;
                nv.MaDonViTinh = this.MaDonViTinh;
                nv.GiaTriThue = this.TenThue;
                nv.MaThue = this.MaThue;
                nv.GiaNhap = txtgianhap.Text;
                nv.GiaBanBuon = txtgiabanbuon.Text;
                nv.GiaBanLe = txtgiabanle.Text;
                nv.MucDatHang = txtmucdathang.Text;
                nv.MucTonToiThieu = txttontoithieu.Text;
                nv.LinkImage = links;
                nv.LinkImageRoot = linksRoot;
                nv.Images = bytes;
                nv.GhiChu = txtGhiChu.Text;
                nv.Deleted = false;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                nv = null;
            }
            return nv;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.HangHoa nv)
        {
            Boolean kt = true;
            try
            {
                if (nv.MaHangHoa == string.Empty)
                {
                    MessageBox.Show("Chưa có mã hàng hóa", "Thông báo");
                    txtmahanghoa.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.TenHangHoa == string.Empty)
                {
                    MessageBox.Show("Nhập tên hàng hóa", "Thông báo");
                    txttenhanghoa.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.TenDonViTinh == string.Empty)
                {
                    MessageBox.Show("Nhập tên đơn vị tính", "Thông báo");
                    cbxdonvitinh.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MaDonViTinh == string.Empty)
                {
                    MessageBox.Show("Nhập mã đơn vị tính", "Thông báo");
                    cbxdonvitinh.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.TenLoai == string.Empty)
                {
                    MessageBox.Show("Nhập tên loại hàng","Thông báo");
                    cbxloaihanghoa.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MaLoai == string.Empty)
                {
                    MessageBox.Show("Nhập mã loại hàng","Thông báo");
                    cbxloaihanghoa.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.TenNhomHang == string.Empty)
                {
                    MessageBox.Show("nhập tên nhóm hàng","thông báo");
                    cbxnhomhanghoa.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MaNhomHang == string.Empty)
                {
                    MessageBox.Show("nhập mã nhóm hàng","thông báo");
                    cbxnhomhanghoa.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MaThue == string.Empty)
                {
                    MessageBox.Show("nhập mã thuế","thông báo");
                    cbxthue.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.GiaTriThue == string.Empty)
                {
                    MessageBox.Show("nhập giá trị thuế ","thông báo");
                    cbxthue.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MucDatHang == string.Empty)
                {
                    MessageBox.Show("Nhập mức đặt hàng", "Thông báo");
                    txtmucdathang.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MucTonToiThieu == string.Empty)
                {
                    MessageBox.Show("nhập mức tồn tối thiểu","thông báo");
                    txttontoithieu.Focus();
                    kt = false;
                    return kt;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = false;
            }
            return kt;
        }
        #endregion

        #region Tim Anh
        private void Search()
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    links = open.SafeFileName.ToString();
                    linksRoot = open.FileName.ToString();
                    bytes = Common.Utilities.GetImage(linksRoot);
                    image = Common.Utilities.GetImage(bytes);
                    picNhanVien.BackgroundImage = image;
                    txtLinkRoot.Text = linksRoot;
                    picNhanVien.BackgroundImageLayout = ImageLayout.Stretch;
                }
                else
                {
                    links = this.nhanvien.LinkImage;
                    linksRoot = this.nhanvien.LinkImageRoot;
                    txtLinkRoot.Text = this.nhanvien.LinkImage;
                    bytes = this.nhanvien.Images;
                    image = this.nhanvien.Picture;
                }
                txtgiabanbuon.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền thêm", "Thông báo");
                    return;
                }
                Entities.HangHoa nv = this.LayBanGhi("Insert");
                if (nv.MaHangHoa != txtmahanghoa.Text)
                {
                    nv = this.LayBanGhi("Insert");
                }
                if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertHangHoa(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = true;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền sửa", "Thông báo");
                    return;
                }
                Entities.HangHoa nv = this.LayBanGhi("Update");
                if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.UpdateHangHoa(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = false;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Event
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            try
            {
                switch (this.hanhDong)
                {
                    case "Them":
                        this.Them();
                        break;
                    case "Sua":
                        this.Sua();
                        break;
                }
            }
            catch (Exception)
            {

            }
        }

        private void txtTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckNumber(e);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            this.Search();
        }
        

        private void cbxloaihanghoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Entities.GetCombobox row = (Entities.GetCombobox)cbxloaihanghoa.SelectedItem;
                this.MaLoaiHang = row.TenCotKhoaChinh;
                this.TenLoaiHang = row.TenCotTenHienThi;
            }
            catch (Exception)
            {
                this.MaLoaiHang = string.Empty;
                this.TenLoaiHang = string.Empty;
            }
        }

        private void cbxnhomhanghoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Entities.GetCombobox row = (Entities.GetCombobox)cbxnhomhanghoa.SelectedItem;
                this.MaNhomHang = row.TenCotKhoaChinh;
                this.TenNhomHang = row.TenCotTenHienThi;
            }
            catch (Exception)
            {
                this.MaNhomHang = string.Empty;
                this.TenNhomHang = string.Empty;
            }
        }

        private void cbxdonvitinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Entities.GetCombobox row = (Entities.GetCombobox)cbxdonvitinh.SelectedItem;
                this.MaDonViTinh = row.TenCotKhoaChinh;
                this.TenDonViTinh = row.TenCotTenHienThi;
            }
            catch (Exception)
            {
                this.MaDonViTinh = string.Empty;
                this.TenDonViTinh = string.Empty;
            }
        }

        private void cbxthue_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Entities.GetCombobox row = (Entities.GetCombobox)cbxthue.SelectedItem;
                this.MaThue = row.TenCotKhoaChinh;
                this.TenThue = row.TenCotTenHienThi;
            }
            catch (Exception)
            {
                this.MaThue = string.Empty;
                this.TenThue = string.Empty;
            }
        }
        #endregion
    }
}
